<template>
  <div>
    <div style="text-align:left">
      患者信息：
      <el-tag>姓名：{{patient.real_name}}</el-tag>
      <el-tag>病历号：{{patient.case_number}}</el-tag>
      <el-tag>年龄：{{patient.age}}</el-tag>
      <el-tag>性别：{{patient.gender}}</el-tag>
    </div>
    <el-divider></el-divider>
    <div style="font-size:20px;text-align:left">
      <i class="el-icon-document-checked">病历首页</i>
    </div>
    <el-divider></el-divider>
    <el-descriptions title="病案首页"  :column="1" border style="margin-top:20px;width:80%">
      <el-descriptions-item label="主诉" :labelStyle="{'width':'120px'}">
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="medical_record.readme" placeholder="请输入主诉"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="现病史">
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="medical_record.present" placeholder="请输入现病史"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="现病治疗情况">
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="medical_record.present_treat" placeholder="请输入现病治疗情况"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="既往史">
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="medical_record.history" placeholder="请输入既往史"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="过敏史">
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="medical_record.allergy" placeholder="请输入过敏史"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="体格检查">
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="medical_record.physique" placeholder="请输入体格检查信息"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="诊断">
        <el-table :data="medical_record.disease" style="width:100%" @selection-change="delDiseaseSelection" ref="selectDiseaseTable">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="disease_code" label="疾病编码" width="180"></el-table-column>
          <el-table-column prop="disease_name" label="疾病名称" width="180"></el-table-column>
          <el-table-column prop="diseaseICD" label="国际ICD编码" width="180"></el-table-column>
          <el-table-column prop="disease_category" label="疾病类型" width="180"></el-table-column>
          <el-table-column >
            <template slot="header" slot-scope="scope">
              <el-button type="text" @click="delDisease">删除</el-button>
              <el-button type="text" @click="dialogTableVisible = true">增加</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-descriptions-item>
      <el-descriptions-item label="检查/检验建议">
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="medical_record.proposal" placeholder="请输入检查/检验建议"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="注意事项">
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="medical_record.careful" placeholder="请输入注意事项"></el-input>
      </el-descriptions-item>
    </el-descriptions>

    <div style="text-align:left;margin-top:20px">
      <el-button type="primary" @click="addNewMedicalRecord()">保存</el-button>
      <el-button type="primary" @click="clearMedicalRecord()">清空</el-button>
    </div>
    <!-- 疾病选择对话框 -->
    <el-dialog title="添加疾病诊断" :visible.sync="dialogTableVisible">
      <div>
        <el-tag style="width:20%">疾病名称:</el-tag><el-input style="width:80%" v-model="disease.disease_name" placeholder="请输入疾病名称"></el-input>
      </div>
      <div>
        <el-tag style="width:20%">疾病助记码:</el-tag><el-input style="width:80%" v-model="disease.disease_code" placeholder="请输入疾病助记码"></el-input>
      </div>
      <div>
        <el-tag style="width:20%">疾病ICD:</el-tag><el-input style="width:80%" v-model="disease.diseaseICD" placeholder="请输入疾病国际ICD编码"></el-input>
      </div>
      <div style="float:right">
        <el-button @click="searchDisease()">搜索</el-button>
      </div>
      <el-table :data="search_disease" @selection-change="diseaseSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column property="disease_name" label="疾病名称" width="150"></el-table-column>
        <el-table-column property="disease_code" label="疾病助记码" width="200"></el-table-column>
        <el-table-column property="diseaseICD" label="疾病ICD"></el-table-column>
      </el-table>
      <div>
        <el-button type="primary" @click="addSelectionDisease()">添加</el-button>
        <el-button type="info" @click="dialogTableVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>

  
</template>

<script>
  import {isNullMessage} from "../../../util/util"
  import qs from 'qs';
  export default {
    methods:{
      //选择删除疾病
      delDiseaseSelection(selVal){
        this.temp_disease = selVal;
      },
      //删除疾病
      delDisease(){
        for(let i=0;i<this.temp_disease.length;i++){
          for(let j=0;j<this.medical_record.disease.length;j++){
            if(this.temp_disease[i].disease_name === this.medical_record.disease[j].disease_name){
              this.medical_record.disease.splice(j,1);
              j--;
            }
          }
        }
      },
      //选择疾病
      diseaseSelectionChange(selVal){
        this.temp_disease = selVal;
      },
      //将选择疾病加到病历中
      addSelectionDisease(){
        // this.dialogTableVisible = false;
        if(this.temp_disease!=null && this.temp_disease.length>0){
          for(let i=0;i<this.temp_disease.length;i++){
            this.medical_record.disease.push(this.temp_disease[i]);
          }          
          // console.log(this.temp_disease[0]['disease_name']);
        }
      },
      //搜索疾病信息
      searchDisease(){
        this.$http.post("http://localhost:8092/getDisease",qs.stringify(this.disease)).then(
          (resp)=>{
            this.search_disease = resp.data;
          }
        ) 
      },
      //保存病历首页
      addNewMedicalRecord(){
        this.medical_record.register_id = this.patient.id;
        this.$http.post("http://localhost:8092/addHomeMedicalRecord",qs.stringify(this.medical_record)).then(
          (resp)=>{            
            this.$message({
              message:"添加病例成功！",
              type:"success"
            });
          }
        );
        this.clearMedicalRecord();
      },
      clearMedicalRecord(){
        this.medical_record = {};
        this.medical_record.disease = [];
      }
    },
    data() {
      return {
        temp_disease:[],//临时保存疾病信息
        search_disease:[],//对话框搜索得到的疾病信息
        disease:{},
        dialogTableVisible:false,
        medical_record:{
          id:'',
          readme:'',//主诉
          present:'',//现病史
          present_treat:'',//现病治疗情况
          history:'',//既往史
          allergy:'',//过敏史
          physique:'',//体格检查
          disease:[],//诊断（对话框选择后的 疾病信息）
          proposal:'',//检查、检验建议          
          careful:'',//注意事项
        },
        patient:{},
      }
    },
    mounted:function(){
      this.patient = JSON.parse(sessionStorage.getItem("record_now_patient"));
      if(isNullMessage(this,this.patient,'请先选择患者')){
        this.patient={
          real_name:'',
          case_number:'',
          age:'',
          gender:''
        }
      }
    }
  }
</script>